var ENDPOINT_PATH = "userAdmin";
layui.extend({
    baseConfig : "/js/pages/baseConfig"
})
layui.use(['form','layer','laydate','table','laytpl','element','util','baseConfig'],function(){

    var form = layui.form
    layer = parent.layer === undefined ? layui.layer : top.layer,
        baseConfig = layui.baseConfig,
        $ = layui.jquery;

    loadRoleSelect();
    //绑定校验
    form.verify({
        roleId : function(val){
            if( !val || val == ''){
                return "员工角色为必选";
            }
        },
        /*nom : function(val){
            if(val == ''){
                return "姓不能为空";
            }
            var rule = /^[A-Z]*$/;
            if( !rule.test( val)){
                return "姓必须全为大写字母";
            }
        },*/
        prenom : function(val){
            if(val == ''){
                return "名不能为空";
            }
            var rule = /^[A-Z].*$/;
            if( !rule.test( val)){
                return "名必须全为大写字母";
            }
        }
    })


    //监听提交
    form.on("submit(submitBtn)",function( data){
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        //提交数据
        $.post( "/" + ENDPOINT_PATH + "/addOrUpdate",{
            id : data.field.id,
            phone : data.field.phone,
            nom : data.field.nom,
            prenom : data.field.prenom,
            email : data.field.email,
            roleId : data.field.roleId,
            jobNumber : data.field.jobNumber,
            collaborateur : data.field.collaborateur,
            reviewStatus : data.field.reviewStatus,
            reviewMemo : data.field.reviewMemo
        },function( res){
            if( res.code == 200){
                setTimeout(function(){
                    top.layer.close( index);
                    top.layer.alert( res.msg);
                    layer.closeAll( "iframe");
                    //刷新父页面
                    parent.location.reload();
                },500);
            }else if( res.code == 60001){
                top.layer.close( index);
                top.layer.msg( res.msg);
            }else if( res.code == 60002){
                top.layer.close( index);
                top.layer.msg( res.msg);
            }else{
                top.layer.close( index);
                top.layer.msg( res.msg);
            }
        });
    });

    //加载角色列表
    function loadRoleSelect() {
        $.post( "/role/list",{},function( res){
            if( res.code == 200){
                var roles = res.data;
                var sel = $("select[name=roleId]");
                for( var i = 0 ; i < roles.length ; i++){
                    var role = roles[i];
                    sel.append( "<option value=\"" + role.id + "\">" + role.roleName + "</option> ")
                }
                //更新渲染
                form.render( "select");
                var roleId = $("#roleIdForValue").val();
                $("#roleSelect").val( roleId);
                form.render( "select");
                //加载完成后，隐藏
                handleRoleFiled( $("#roleIdForValue").val());
            }else{
                top.layer.msg( res.msg);
            }
        });
    }

    form.on('select(roleSelect)', function( data){
        handleRoleFiled( data.value);
    });

    //员工才有的信息，管理员没有
    //collaborateur
    //number   数据库角色id 3以上 是员工帐号
    function handleRoleFiled( value){
        if( parseInt( value) > 3){
            $( "input[name=collaborateur]").attr( "placeholder", "请输入collaborateur").attr( "readonly", false);
        }else{
            $( "input[name=collaborateur]").val( "");
            $( "input[name=collaborateur]").attr( "placeholder", "管理员不需要collaborateur").attr( "readonly", true);
        }
    }
});
